﻿///***************************************************************
/// 시  스  템 : Updms.Crd
/// 단위시스템 : CRD
/// 계      층 : Presentation Layer
/// 프로그램명 : Crd4011b
/// 파  일  명 : Crd4011b.aspx.cs
/// 설      명 : 카드사용장소 일괄업데이트
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2011-10-18 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.SMGridView;

using Base.Dac;
using Updms.Crd.Exe;


namespace Updms.Crd
{
	public partial class Crd4011b : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Crd4011b";
		public static readonly string gs_path_info = "/updms/Crd/" + PID + ".aspx";

		Global global = new Global();

		//검색
		protected string kw1 = "";	//카드
		protected string kw2 = "";	//내용

		//메뉴 선택값
		protected string gs_sm = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetCardInfo();
			SetContentsList();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetInit, SetSearchParameter, ShowKeywordLabel

		//초기값설정
		private void SetInit()
		{
			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			string ls_card_cd = "";
			for (int i = 0; i < cbl_search_card_cd.Items.Count; i++)
			{
				if (cbl_search_card_cd.Items[i].Selected == true)
				{
					ls_card_cd += cbl_search_card_cd.Items[i].Value + ",";
				}
			}
			kw1 = ls_card_cd.Substring(0, ls_card_cd.LastIndexOf(","));
			kw2 = ddl_search_contents.SelectedValue;
		}

		//검색키워드
		private void ShowKeywordLabel()
		{
			string ls_kw1 = WebControlUtil.GetCheckBoxListText(cbl_search_card_cd);
			lbl_search_keyword.Text = ls_kw1 + " / " + kw2;
		}

		#endregion

		#region 마스터 Method (SetCardInfo, SetContentsList)

		//카드정보
		private void SetCardInfo()
		{
			DataSet lo_ds = new DataSet();
			Crd1010m_NT _Crd1010m_NT = new Crd1010m_NT();

			lo_ds.Merge(_Crd1010m_NT.GetCardInfoList("A"));

			cbl_search_card_cd.Items.Clear();
			cbl_search_card_cd.DataSource = lo_ds;
			cbl_search_card_cd.DataMember = "UPDMS_CRD_INFO";
			cbl_search_card_cd.DataTextField = "Card_Nm_Color";
			cbl_search_card_cd.DataValueField = "Card_Cd";
			cbl_search_card_cd.DataBind();

			if (cbl_search_card_cd.Items.Count > 0)
			{
				WebControlUtil.SelectCheckBoxList(cbl_search_card_cd, true);
			}
		}

		//결제내용
		private void SetContentsList()
		{
			DataSet lo_ds = new DataSet();
			Crd4011b_NT _Crd4011b_NT = new Crd4011b_NT();

			lo_ds.Merge(_Crd4011b_NT.GetContentsList());

			ddl_search_contents.Items.Clear();
			ddl_search_contents.DataSource = lo_ds;
			ddl_search_contents.DataMember = "UPDMS_CRD_CONTENTS";
			ddl_search_contents.DataTextField = "Text";
			ddl_search_contents.DataValueField = "Value";
			ddl_search_contents.DataBind();

			ltr_search_contents_cnt.Text = "(" + ddl_search_contents.Items.Count + ")";
		}

		//새로고침
		private void RefreshContents()
		{
			OnPageLoad();
		}

		#endregion	

		#region CRUD (SelectProcess, UpdateProcess, SearchProcess)

		//조회
		private void SelectProcess()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();
			
			//검색
			lo_dp.AddProperty("ls_card_cd", typeof(string), kw1);
			lo_dp.AddProperty("ls_contents", typeof(string), kw2);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			try
			{
				using (Crd4011b_NT _Crd4011b_NT = new Crd4011b_NT())
				{
					lo_ds.Merge(_Crd4011b_NT.GetCardUsingLocation(lo_dp));
				}
				GridView1.DataSource = lo_ds;
				GridView1.DataBind();

				//결과출력(키워드, 검색행수)
				ShowKeywordLabel();
				lbl_result_count.Text = DataSetUtil.GetRowCount(lo_ds, "UPDMS_CRD_BATCH").ToString();
			}
			catch (Exception ex)
			{
				CallExceptionMsgBoxInAjax(ex);
			}
		}

		//수정
		private void UpdateProcess()
		{
			try
			{
				using (Crd4011b_RT _Crd4011b_RT = new Crd4011b_RT())
				{
					int li_return = _Crd4011b_RT.UpdateCardUsingLocation(SetValue());
					string ls_msg_id = (li_return > 0) ? "MSG_INF_006" : "MSG_ERR_104";	//수정되었습니다:수정할 수 없습니다
					CallMsgBoxInAjax(ls_msg_id);

					SearchProcess();
				}
			}
			catch (Exception ex)
			{
				CallExceptionMsgBoxInAjax(ex);
			}
		}

		//검색
		private void SearchProcess()
		{
			SetSearchParameter();
			ClearValue();

			SelectProcess();
		}

		#endregion		

		#region SetValue, ClearValue

		//SetValue
		private DataPack SetValue()
		{
			DataPack lo_dp = new DataPack();
			string ls_seq_mass = "";
			string ls_seq_ins = "";

			for (int i = 0; i < GridView1.Rows.Count; i++)
			{
				GridViewRow row = GridView1.Rows[i];
				HtmlInputCheckBox cb = (HtmlInputCheckBox)row.FindControl("cb_check");

				if (cb.Checked)
				{
					string ls_seq = GridView1.DataKeys[i].Values["Seq"].ToString();
					string ls_biz_div = GridView1.DataKeys[i].Values["Biz_Div"].ToString();

					if (ls_biz_div == "AB003") //할부
					{
						ls_seq_ins += ls_seq + ",";
					}
					else
					{
						ls_seq_mass += ls_seq + ",";
					}
				}
			}

			lbl_seq_mass.Text = (ls_seq_mass.Length > 0) ? ls_seq_mass.Substring(0, ls_seq_mass.LastIndexOf(",")) : "";
			lbl_seq_ins.Text = (ls_seq_ins.Length > 0) ? ls_seq_ins.Substring(0, ls_seq_ins.LastIndexOf(",")) : "";

			lo_dp.AddProperty("ls_seq_mass", typeof(string), lbl_seq_mass.Text);
			lo_dp.AddProperty("ls_seq_ins", typeof(string), lbl_seq_ins.Text);
			lo_dp.AddProperty("ls_latitude", typeof(string), ConversionUtil.DefaultValue(txt_latitude.Text, ""));
			lo_dp.AddProperty("ls_longitude", typeof(string), ConversionUtil.DefaultValue(txt_longitude.Text, ""));
			lo_dp.AddProperty("ls_update_user", typeof(string), USER_ID);

			return lo_dp;
		}

		//ClearValue
		private void ClearValue()
		{
			h_no.Value = "";
			lbl_seq_mass.Text = "";
			lbl_seq_ins.Text = "";
			txt_latitude.Text = "";
			txt_longitude.Text = "";
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				try
				{
					using (AccessCheck access = new AccessCheck())
					{
						//접근권한 체크
						if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
						{
							Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
						}
					}
				}
				catch (Exception ex)
				{
					CallExceptionMsgBoxInAjax(ex);
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		//RowDataBound
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			GridView gv = (GridView)sender;
			
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//번호
				e.Row.Cells[1].Text = (e.Row.RowIndex + 1).ToString();

				//마우스올렸을 때
				e.Row.Attributes.Add("onmouseover", "overBackground('GridView1', " + (e.Row.RowIndex + 1) + ")");
				e.Row.Attributes.Add("onmouseout", "outBackground('GridView1', 1)");
			}

			//머지
			SMGridView sGv = new SMGridView(this.Page, ref GridView1);
			sGv.RowSpanGridView(GridView1, 4);//카드명
			sGv.RowSpanGridView(GridView1, 6);//결제구분
		}

		//RowCommand
		protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);

			h_no.Value = row.Cells[1].Text.ToString();			//1.번호
			txt_latitude.Text = row.Cells[10].Text.ToString();	//10.Latitude
			txt_longitude.Text = row.Cells[11].Text.ToString();	//11.Longitude
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		//저장
		protected void ibtn_save_Click(object sender, EventArgs e)
		{
			UpdateProcess();
		}

		//새로고침
		protected void lbtn_refresh_Click(object sender, EventArgs e)
		{
			RefreshContents();
		}

		#endregion

	}
}
